package com.leetcode.hot100;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode.hot100
 * @Author: lei
 * @CreateTime: 2024-03-19  14:32
 * @Description: 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 * @Version: 1.0
 */
public class 电话号码的字母组合_17 {
    public static void main(String[] args) {
        System.out.println(letterCombinations("23"));
    }


    public static List<String> letterCombinations(String digits) {
        String[] letter_map = {" ", "*", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        List<String> arrayList = new ArrayList<String>();
        if (!digits.isEmpty()) {
            produceList(letter_map, arrayList, digits, new StringBuilder(), 0);
        }
        return arrayList;
    }

    private static void produceList(String[] letter_map, List<String> arrayList, String digits, StringBuilder stringBuilder, int index) {

        if (index == digits.length()) {
            arrayList.add(String.valueOf(stringBuilder));
            return;
        }
        int start = digits.charAt(index) - '0';
        String str = letter_map[start];
        for (int i = 0; i < str.length(); i++) {
            stringBuilder.append(str.charAt(i));
            produceList(letter_map, arrayList, digits, stringBuilder, index + 1);
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
    }
}

